Ext.namespace("Ext.qd");
Ext.namespace("Ext.qd.nzb");
Ext.qd.nzb.feedtab = Ext.extend(Ext.grid.GridPanel, {
    initComponent : function() {
    this.expander = new Ext.grid.RowExpander({
      tpl : new Ext.Template('{ItemDescription}')
    });
    this.cellActions = new Ext.ux.grid.CellActions({
      listeners:{
        action: function(grid, record, action, value) {
        }
        ,beforeaction:function() {
        }
      }
      ,align:'left'
    });
    Ext.apply(this, {   
      store: new Ext.data.JsonStore({
        url      : 'proxy.php',
        baseParams : {
          exw_action : 'QDNzbProxy.dbfeed'            
        },
        root: 'feeds',
        fields: [
           {name: 'ItemTitle'},
           {name: 'ItemDescription'}
       ],
        listeners : {
          load  : function ( store, records ){
            Ext.each(records,function (r,i){
            });              
          }
        }
      }),
      plugins : [this.cellActions,this.expander],
      columns: [
          this.expander,      
          {header: "Title" , width:  300, dataIndex: 'ItemTitle'   , sortable: true, renderer : function(a,b,c){
            if (c.data.ItemDescription){
              var v;
              var rgx = new RegExp("Taille : (.*?)\<br","gi")
              if (v = rgx.exec(c.data.ItemDescription)) c.data.taille = v[1];
              var rgx = new RegExp("Nom du fichier : (.*?)\<br","gi")
              if (v = rgx.exec(c.data.ItemDescription)) c.data.filename = v[1];
            }  
            return '<b>'+c.data.ItemTitle+'</b> '+c.data.taille;
          }}          
      ]
    });
    this.on ({
      rowdblclick  : function ( grid, rowIndex, e ){
        Ext.getCmp('txt2search').setValue(grid.store.data.items[rowIndex].data.filename);
        Ext.getCmp('btnSearch').events.click.listeners[0].fn();
      },
      contextclick : function(link){
      }
    });
    Ext.qd.nzb.feedtab.superclass.initComponent.apply(this, arguments);
  }
});   
Ext.reg('nzb.feedtab',Ext.qd.nzb.feedtab);


Ext.qd.nzb.NzbTab = Ext.extend(Ext.grid.GridPanel, {
  initComponent : function() {
    this.cellActions = new Ext.ux.grid.CellActions({
      listeners:{
        action: function(grid, record, action, value) {
        }
        ,beforeaction:function() {
        }
      }
      ,align:'left'
    });
    var sm = new Ext.grid.CheckboxSelectionModel();
    Ext.apply(this, {   
      store: new Ext.data.JsonStore({
          url      : 'proxy.php',
          baseParams : {
            exw_action : 'QDNzbProxy.search',
          },
          autoLoad : false,
          root: 'posts',
          fields: [
             {name: 'id'     , mapping: 'id'},
             {name: 'title'  , mapping: 'title'},
             {name: 'group'  , mapping: 'group'},
             {name: 'percent'  , mapping: 'percent'},
             {name: 'size'  , mapping: 'size'},
             {name: 'age'    , mapping: 'age'}
             
         ],
          sortInfo:{field: 'age', direction: "ASC"},
      }),
      plugins : [this.cellActions],
      columns: [
           sm,
          {header: "id"      , width:  70, dataIndex: 'id'      , sortable: true},
          {header: "size"    , width:  75, dataIndex: 'size'    , sortable: true},
          {header: "percent" , width:  60, dataIndex: 'percent' , sortable: true},
          {header: "age"     , width:  60, dataIndex: 'age'     , sortable: true},
          {header: "group"   , width:  80, dataIndex: 'group'   , sortable: true},
          {header: "Nom"     , width: 400, dataIndex: 'title'   , sortable: true}
      ],
      selMode : sm
    });
    this.on ({
      rowdblclick  : function ( grid, rowIndex, e ){
        var record = grid.getStore().getAt(rowIndex);
      },
      contextclick : function(link){
      }
    });
    Ext.qd.nzb.NzbTab.superclass.initComponent.apply(this, arguments);
  }
});   

  
  
Ext.qd.nzb.NZBPanel = Ext.extend(Ext.Panel, {
  initComponents : function(){
  Ext.apply(this,{
    layout    : 'border',
    items     : [{
      region    : 'north',
      xtype     : 'panel',
      layout    : 'table',
      height    : 30,
      layoutConfig: {
        columns   : 6
      },
      items     :[{
        xtype     : 'textfield',
        id        : 'txt2search',
        width     : 300,
        enableKeyEvents :true,
        height    : 20,
        listeners: { 
          keypress: function(ob,e){
            if (e.getCharCode()==13){
              Ext.getCmp('btnSearch').events.click.listeners[0].fn();
            }
          }
        }  
      },{
        xtype     : 'button',
        text      : 'search',
        id        : 'btnSearch',
        width     : 100,
        listeners : {
          click : function(){ 
            var activeTab = Ext.getCmp('tabs').getItem('tab-'+Ext.getCmp('txt2search').getValue());
            if (!activeTab){
              activeTab = new Ext.qd.nzb.NzbTab ({              
                title    : '&nbsp;'+Ext.getCmp('txt2search').getValue(),
                id       : 'tab-'+Ext.getCmp('txt2search').getValue(),
                closable : true
              });
              Ext.getCmp('tabs').add(activeTab);
              activeTab.q =Ext.getCmp('txt2search').getValue(); 
            }
            Ext.getCmp('tabs').setActiveTab(activeTab);
            activeTab.store.load({
              params :{
                q  : activeTab.q,
                dc : Math.random(),
                s  : Ext.getCmp('comboSearcher').getValue()         
              }
            });
          }
        }
      },{
        xtype     : 'button',
        text      : 'none',
        width     : 100,
        listeners : {
          click : function(){
            Ext.getCmp('tabs').getItem(Ext.getCmp('tabs').getActiveTab().id).getSelectionModel().clearSelections();          
          }
        }
      },{
        xtype     : 'button',
        text      : 'all',
        width     : 100,
        listeners : {
          click : function(){
            Ext.getCmp('tabs').getItem(Ext.getCmp('tabs').getActiveTab().id).getSelectionModel().selectAll();          
          }
        }
      },{
        xtype     : 'button',
        text      : 'download',
        width     : 100,
        listeners : {
          click : function(){
            var idsString = '';
            var sepa = '';
            var activeTab = Ext.getCmp('tabs').getItem(Ext.getCmp('tabs').getActiveTab().id);
            var l = activeTab.getSelectionModel().getSelections();
            Ext.each(l,function(record){
              idsString = idsString + sepa + record.data.id; 
              sepa='!';
            })
        
            Ext.Ajax.request({
              url    : 'proxy.php',
              success: function(r,a){
                activeTab.setTitle('DWN '+activeTab.q);
              },
              params: {
                exw_action : 'QDNzbProxy.download',
                ids        : idsString,
                q          : activeTab.q,
                s          : Ext.getCmp('comboSearcher').getValue()
              }
           });            
          }
        }
      },{
        xtype     : 'combo',
        id        : 'comboSearcher',
        store     : new Ext.data.SimpleStore({
            fields  : ['it'],
            data    : [['binsearch'],['newzleech']]
        }),
        typeAhead     : true,
        forceSelection: true,
        selectOnFocus : true,
        displayField  : 'it',
        mode          : 'local',
        triggerAction : 'all',
        value         : 'binsearch'        
      }]
    },{
      region   : 'center',
      xtype    : 'tabpanel',
      id       : 'tabs',
      width    : 300,
      activeTab: 0,
      items      : [{
        xtype      : 'panel',
        html       : 'search',
        title      : 'search'
      }]
    },{
      stype : 'panel',  
      region: 'west',
      layout: 'border',
      width : 350,
      split : true,
      items : [{
          region :'north',
          xtype  :'panel',
          border : false,
          autoWidth    : true,
          height : 25,            
          items    : {
            xtype        : 'combo',
            autoWidth    : true,
            id           : 'combofeed',
            displayField : 'ItemSource',
            valueField   : 'ItemSource',
            allowBlank   : false,
            mode         : 'local',
            store        : new Ext.data.JsonStore({
              url          : 'proxy.php',
              baseParams     : {
                exw_action : 'QDNzbProxy.distinct_feed',
              },
              autoLoad     : true,
              root         : 'res',
              fields       : [{name: 'ItemSource'}],
              sortInfo     : {field: 'ItemSource', direction: "ASC"},
              listeners    : {
                load         : function(){
                  var firstValue = Ext.getCmp('combofeed').store.data.items[0].data.ItemSource;
                  Ext.getCmp('combofeed').setValue(firstValue);
                  Ext.getCmp('feedtab').store.load({
                    params : {
                      is     : firstValue
                    }
                  });
                }
              }
            }),
            listeners : {
              select    : function(field,record,idx){
                Ext.getCmp('feedtab').store.load({
                  params : {
                    is     : record.data.ItemSource
                  }
                });
              }
            },
            forceSelection : true,
            triggerAction  : 'all'
          }
        },new Ext.qd.nzb.feedtab({
          region:'center',
          id    :'feedtab'
      })]    
    }]
  });
  Ext.qd.nzb.NZBPanel.superclass.initComponent.apply(this, arguments);
  }
});
Ext.reg('nzb.NZBPanel',Ext.qd.nzb.NZBPanel)




var VLCPlayerPanel = null;

Ext.onReady(function(){
  Ext.QuickTips.init();
  //wVLCPlayerPanel=null;
  //Ext.qd.nzb.createNzbPanel('divGridNzb');
  var viewport = new Ext.Viewport({
	  layout: 'fit',
	  renderTo: Ext.getBody(),
	  items 	: {
		  xtype		: 'tabpanel',
	      activeTab : 2,
	      items     : [{
	          title : 'Main',
	          xtype : 'panel',
	          html  : 'NZB & movies'   
	      },{
	          title : 'NZB',
	          xtype : 'nzb.NZBPanel'
	      },{
	          title : 'Series',
	          xtype : 'nzb.SeriesPanel'
	      },{
	          title : 'Movies',
	          xtype : 'nzb.moviePanel'
	      }]
  	  }
  });
  //Ext.qd.nzb.createMoviesPanel('divGridNzb');
  //Ext.qd.nzb.chooseMovie({data : {title : 'Dead Like Me Life After Death'}})
  
  //VLCOpenVideo("http://streams.videolan.org/streams-videolan/mp4/Mr_MrsSmith-h264_aac.mp4");
  
  //VLCOpenVideo('file:////zutzut/m/####toextract/U-571.DVDRip.FR/U-571.cd1.DivX.DVDRip.FR.AVI');
});